package com.bailan.foreat.controller;


import com.bailan.foreat.entity.Product;
import com.bailan.foreat.entity.Seller;
import com.bailan.foreat.entity.Store;
import com.bailan.foreat.service.ProductService;
import com.bailan.foreat.service.SellerService;
import com.bailan.foreat.vo.EsProduct;
import com.bailan.foreat.vo.EsStore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bailan.foreat.utils.CookieUtils;
import com.bailan.foreat.utils.JSONUtils;
import com.bailan.foreat.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    SellerService sellerService;
    @Autowired
    ProductService productService;

    @RequestMapping("add")
    @ResponseBody
    public String add(Integer storeId, Product product,@RequestParam("chooseTypes[]") Integer[] chooseTypes){

        product.setBuyCount(0);
        product.setStatus(0);
        productService.addProduct(storeId,product,chooseTypes);

        return "ok";

    }


    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer productId){
        productService.deleteProduct(productId);
        return "ok";
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Product product){
        productService.updateProduct(product);
        return "ok";
    }

    @RequestMapping("select")
    @ResponseBody
    public Page<Product> select(Integer storeId,Integer currentPage, Integer pageSize, @RequestParam(defaultValue = "") String keyName) {
        Page proPage;
        if ("".equals(keyName)) {
            proPage = productService.selectProduct(storeId,currentPage,pageSize);
        } else {
            Page<EsProduct> esProductPage = productService.searchByES(currentPage, pageSize, keyName);
            proPage = esProductPage;
        }

        return proPage;
    }

    @RequestMapping("sa")
    public String sa(Integer storeId,String storeName,Model model){
        model.addAttribute("storeId", storeId);
        model.addAttribute("storeName", storeName);
        //System.out.println("1...."+storeId);
        //System.out.println(storeName);
        return "productPage";
    }

    @RequestMapping("toProductDetail")
    public String toProductDetail(Integer productId, Model model) {
        model.addAttribute("productId", productId);
        return "productdetail";
    }

    @RequestMapping("queryProductDetail")
    @ResponseBody
    public Product queryProductDetail(Integer productId) {
        return productService.selectById(productId);
    }

}

